package com.cskaoyan.aspect;

import com.cskaoyan.bean.Log;
import com.cskaoyan.mapper.LogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author stone
 * @date 2022/11/14 20:43
 */
@Aspect
@Component
public class LogAspect {

    @Autowired
    LogMapper logMapper;

    @Around("execution(* com..service..*(..))")
    public Object logAround(ProceedingJoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        String operation = className + "." + methodName;
        Log log = new Log(null, operation, new Date(), true);

        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            log.setSuccess(false);
            throwable.printStackTrace();
        }finally {
            logMapper.insert(log);
        }
        return proceed;
    }
}
